<?php
include "otamu_config.php";

define("_VIEWPATH_","views/murid/");
$layout = "layout";

$OBJECT = 'master_murid';
$OBJECT_TABLE = $OBJECT;

if (!isset($_GET['act'])) $_GET['act'] = ''; 

class Institusi extends ADOdb_Active_Record
{
	var $_table = "master_murid";
}
 
switch ($_GET['act']) {

	case "home" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "list" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "search_murid" :
		if (empty($_REQUEST['q'])) {
			$res_count = 0;
		}
		else {
			$sql = "SELECT m.id,m.nama FROM master_murid m 
					 LEFT JOIN ref_negeri n ON n.id=i.negeri_id
					 LEFT JOIN ref_daerah d ON d.id=i.daerah_id
					 WHERE i.negeri_id=n.id AND nama ilike '%$_REQUEST[q]%' 
					 ORDER BY id $range";
					 //echo $sql;exit;
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		break;

	case "list_sek" :
		if (empty($_REQUEST['p'])) {
			$page = 1;
			$range = "OFFSET 0 LIMIT 20";
		}
		else {
			$page = $_REQUEST['p'];
			$offset = ($page-1) * 20;
			$range = "OFFSET $offset LIMIT 20";			
		}

		$sql = "SELECT  m.id,m.nama FROM master_murid m 
		        FROM master_institusi i 
		         LEFT JOIN ref_negeri n ON n.id=i.negeri_id
		         LEFT JOIN ref_daerah d ON d.kod_daerah=i.kod_daerah
		         LEFT JOIN ref_kategori_institusi a ON a.id=i.kategori_institusi_id
				 WHERE i.negeri_id=n.id ORDER BY id $range";
		//$sql = "select i.id,i.nama,i.aktif,i.daerah_id,i.alamat from master_institusi i"; 
		//$res = pg_query($sql);

		break;

	case "view_murid" :
		$institusi = new Institusi();

		if (empty($_REQUEST['id'])) {
			$kod = $_REQUEST['kod'];
			$sql = "select id from master_institusi where kod='$kod'";
			$res = pg_query($sql);
			if (pg_num_rows($res)==1) {
				$dat = pg_fetch_object($res,0);
				$institusi->Load('id='.$dat->id);
			}
		}
		else {
			$institusi->Load('id='.$_REQUEST['id']);
		}
		break;

	case "new_murid" :
		break;

	case "create_murid" :
		if ($_REQUEST[$OBJECT]['nama']=='') exitError("Sila Masukkan Nama Murid");
		//check if sub item already exist
				
		$obj = new Institusi();
		
		foreach ($_REQUEST[$OBJECT] as $k=>$v) {
			$obj->$k = $v;
		}
		//print_r($obj);exit;
		
		if (!$obj->save()) { 
			echo $obj->ErrorMsg();exit;
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Institusi telah diwujudkan.';
		}
		//print_r($_REQUEST);
		//exit;
		header("Location: ?act=view_sek&kod=$obj->kod");
		break;
		
	case "edit_sek" :
		$institusi = new Murid();

		if (empty($_REQUEST['id'])) {
			$kod = $_REQUEST['kod'];
			$sql = "select id from master_institusi where kod='$kod'";
			$res = pg_query($sql);
			if (pg_num_rows($res)==1) {
				$dat = pg_fetch_object($res,0);
				$institusi->Load('id='.$dat->id);
			}
		}
		else {
			$institusi->Load('id='.$_REQUEST['id']);
		}
		break;
		
	case "update_sek" :
		$institusi = new Institusi();
		$institusi->Load('id='.$_REQUEST['id']);		

		foreach ($_REQUEST['master_institusi'] as $k=>$v) {
			$institusi->$k = $v;
		}
		if ($_REQUEST['tarikh_tubuh']=='') {
			$institusi->tarikh_tubuh = Date("Y-m-d");
		}
		//print_r($obj);exit;
		
		if (!$institusi->save()) { 
			exitError($institusi->ErrorMsg());
			exit;
			$_SESSION['flash'] = $institusi->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Institusi telah dikemaskini.';
			header("Location: ?act=view_sek&id=$institusi->id");
		}
		break;

	case "confirm_sek" :
		break;

	case "delete_sek" :
		$sql = "DELETE from master_institusi where id='$_REQUEST[id]'";
		if ($res = pg_query($sql)) {
			$_SESSION['flash'] = 'Institusi telah dipadam.';
			header("Location: ?act=list_sek");		
		}
		else {
			exitError(pg_last_error());
		}

		break;



	default :
		//include _VIEWPATH_."home.php";
		$act = "home";

}
include _VIEWPATH_."$layout.php";

?>
